package com.fc.test.controller.myself;

import com.fc.test.controller.myself.vo.CategoryVo;
import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import com.fc.test.util.SnowflakeIdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class MySelfController extends CommController{
    @Autowired
    ForeUserMapper userMapper;


    @RequestMapping("/Fore/user/login")
    public void login(String username,String password) throws IOException {
        ForeUserExample example=new ForeUserExample();
        example.or().andUsernameEqualTo(username);
        example.or().andUsernameEqualTo(password);
        List list =userMapper.selectByExample(example);
        if(list.size()!=0){
            session.setAttribute("user",list.get(0));
            response.sendRedirect("/static/myself/index.html");
        }else{
            response.sendRedirect("/static/myself/register.html");
        }
    }
    @RequestMapping("/Fore/user/guestLogin")
    public void guestLogin() throws IOException {
        ForeUserExample example=new ForeUserExample();
        example.or().andUsernameEqualTo("guest");
        example.or().andUsernameEqualTo("guest");
        List list =userMapper.selectByExample(example);
        session.setAttribute("user",list.get(0));
        response.sendRedirect("/static/myself/index.html");
    }




    @RequestMapping("/Fore/user/register")
    public void register(String username,String password) throws IOException {
        ForeUser user=new ForeUser();
        user.setId(SnowflakeIdWorker.getUUID());
        user.setUsername(username);
        user.setPassword(password);
        ForeUserExample example=new ForeUserExample();
        example.or().andUsernameEqualTo(username);
        List list =userMapper.selectByExample(example);
        if(list.size()>0){
            response.sendRedirect("/static/myself/register.html");
            return;
        }
        userMapper.insertSelective(user);
        response.sendRedirect("/static/myself/success.html");
    }
    @Autowired
    ForeCategoryMapper categoryMapper;
    @Autowired
    ForeFoodMapper foreFoodMapper;


    @RequestMapping("/Fore/category/findCategories")
    public List<CategoryVo> findCategories(){
        List<ForeCategory> list=categoryMapper.selectByExample(null);
        List<CategoryVo> categories=new ArrayList<>();
        for(ForeCategory category:list){
            CategoryVo categoryVo=new CategoryVo();
            BeanUtils.copyProperties(category,categoryVo);
            String cid=category.getId();
            ForeFoodExample foodExample=new ForeFoodExample();
            foodExample.or().andCidEqualTo(cid).andIsDeleteEqualTo("是");
            List<ForeFood> foods=foreFoodMapper.selectByExample(foodExample);
            categoryVo.setFoods(foods);
            categories.add(categoryVo);
        }
        return categories;
    }


    @RequestMapping("/Fore/card/add")
    public void addCard(String foodId){
        Map<String,List<ForeFood>> cart= (Map<String, List<ForeFood>>) session.getAttribute("cart");
        if(cart==null)cart=new HashMap<>();
        ForeFood foreFood=foreFoodMapper.selectByPrimaryKey(foodId);
        List<ForeFood> foreFoods=cart.get(foodId);
        if(foreFoods==null)foreFoods=new ArrayList<>();
        foreFoods.add(foreFood);
        cart.put(foodId,foreFoods);
        session.setAttribute("cart",cart);
        System.out.println(cart.get(foodId).size());
    }

    @RequestMapping("/Fore/user/query")
    public ForeUser queryUser(){
        return (ForeUser) session.getAttribute("user");
    }

    @RequestMapping("/Fore/cart/list")
    public Map queryCartInfo(){
        Map<String,List<ForeFood>> cart= (Map<String, List<ForeFood>>) session.getAttribute("cart");
        List list=new ArrayList<>();
        double total=0.0;
        double discountTotal=0.0;
        for(String key:cart.keySet()){
            List<ForeFood> foreFoods=cart.get(key);
            Map map=new HashMap();
            map.put("food",foreFoods.get(0));
            map.put("size",foreFoods.size());
            map.put("subTotal",foreFoods.get(0).getPrice()*foreFoods.size());
            list.add(map);
            total+=foreFoods.get(0).getPrice()*foreFoods.size();
            discountTotal+=foreFoods.get(0).getPrice()*foreFoods.size()*foreFoods.get(0).getMemberDiscount();
        }
        Map result=new HashMap();
        result.put("list",list);
        result.put("total",total);
        if(queryUser()==null||queryUser().getUsername().equals("guest")){
            result.put("discountTotal",total);
        }else{
            result.put("discountTotal",discountTotal);
        }
        return result;
    }

    @RequestMapping("/Fore/food/findFoodById")
    public ForeFood  findFoodById(String id){
        return foreFoodMapper.selectByPrimaryKey(id);
    }
   @Autowired
   public ForeEvaluateFoodMapper evaluateFoodMapper;

   @RequestMapping("/Fore/comment/findCommentsByFoodId")
   public List<ForeEvaluateFood> findCommentsByFoodId(String id){
       ForeEvaluateFoodExample evaluateFoodExample=new ForeEvaluateFoodExample();
       evaluateFoodExample.or().andFoodIdEqualTo(id);
       return evaluateFoodMapper.selectByExample(evaluateFoodExample);
   }

   @RequestMapping("/Fore/comment/add")
    public void commentAdd(String foodId,String comment,String status){
       ForeEvaluateFood evaluateFood=new ForeEvaluateFood();
       ForeUser user= (ForeUser) session.getAttribute("user");
       if(user!=null){
           evaluateFood.setUserId(user.getId());
           evaluateFood.setUsername(user.getUsername());
       }
       evaluateFood.setId(SnowflakeIdWorker.getUUID());
       evaluateFood.setFoodId(foodId);
       evaluateFood.setFoodName(foreFoodMapper.selectByPrimaryKey(foodId).getName());
       evaluateFood.setMessage(comment);
       evaluateFood.setStatus(status);
       evaluateFoodMapper.insertSelective(evaluateFood);
   }
   @Autowired
    ForeOrderMapper foreOrderMapper;


   @RequestMapping("/Fore/order/generate")
    public void orderGenerate(){
       Map data=queryCartInfo();
       ForeUser user=queryUser();
       List<Map> list= (List<Map>) data.get("list");
       StringBuffer sb=new StringBuffer();
       sb.append("商品条目:\n");
       for (Map temp:list){
           sb.append(((ForeFood)temp.get("food")).getName()+"*"+temp.get("size")+"="+temp.get("subTotal")+"元\n");
       }
       sb.append("总计:"+data.get("total"));
       ForeOrder order=new ForeOrder();
       order.setId(SnowflakeIdWorker.getUUID());
       order.setName(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())+user.getUsername());
       order.setDetail(sb.toString());
       order.setTotal((Double) data.get("discountTotal"));
       order.setType("普通订单");
       order.setStatus("待支付");
       order.setRemark(user.getId());
       foreOrderMapper.insertSelective(order);
       session.setAttribute("cart",null);
   }
   @RequestMapping("/Fore/employ/list")
   public List<ForeEmploy> employs(){
       return employMapper.selectByExample(null);
   }

   @RequestMapping("/Fore/order/list")
    public List<ForeOrder> orders(){
       ForeOrderExample example=new ForeOrderExample();
       example.or().andRemarkEqualTo(queryUser().getId());
       return foreOrderMapper.selectByExample(example);
   }
   @Autowired
   ForeEmployMapper employMapper;
   @Autowired
   ForeEvaluateEmployMapper evaluateEmployMapper;

   @RequestMapping("/Fore/order/addComment")
    public void addOrderComment(String id,String comment,String employId,String commentEmploy,String employStatus){
       //商家评论
       ForeOrder foreOrder=foreOrderMapper.selectByPrimaryKey(id);
       foreOrder.setRestaurantMessage(comment);
       foreOrderMapper.updateByPrimaryKeySelective(foreOrder);
       //店员评论
       ForeEvaluateEmploy foreEvaluateEmploy=new ForeEvaluateEmploy();
       foreEvaluateEmploy.setId(SnowflakeIdWorker.getUUID());
       foreEvaluateEmploy.setEmployId(employId);
       foreEvaluateEmploy.setEmployName(employMapper.selectByPrimaryKey(employId).getName());
       foreEvaluateEmploy.setMessage(commentEmploy);
       foreEvaluateEmploy.setStatus(employStatus);
       foreEvaluateEmploy.setUserId(queryUser().getId());
       foreEvaluateEmploy.setUsername(queryUser().getUsername());
       foreEvaluateEmploy.setType("用户评论");
       evaluateEmployMapper.insertSelective(foreEvaluateEmploy);


    }

    @RequestMapping("/Fore/order/urgent")
    public void orderUrgent(String id){
        ForeOrder foreOrder=foreOrderMapper.selectByPrimaryKey(id);
        foreOrder.setType("加急订单");
        foreOrderMapper.updateByPrimaryKeySelective(foreOrder);
    }

    @RequestMapping("/Fore/order/pay")
    public void orderPay(String id){
        ForeOrder foreOrder=foreOrderMapper.selectByPrimaryKey(id);
        foreOrder.setStatus("已支付");
        foreOrderMapper.updateByPrimaryKeySelective(foreOrder);
    }



}
